Code for the ATM System
Write the object-oriented code to implement the design of the ATM problem.
We've covered different aspects of the ATM and observed the attributes attached to the problem using various UML diagrams. Let's now explore the more practical side of things where we will work on implementing the ATM using multiple languages. This is usually the last step in an object-oriented design interview process.
We have chosen the following languages to write the skeleton code of the different classes present in the ATM system:
Java
C#
Python
C++
JavaScript
ATM classes#
In this section, we will provide the skeleton code of the classes designed in the class diagram lesson.
Note: For simplicity, we are not defining getter and setter functions. The reader can assume that all class attributes are private and accessed through their respective public getter methods and modified only through their public method functions.
Enumerations #
The following code provides the definition of the enumeration used in the ATM system.
ATMState
: This enumeration keeps track of the following states of an ATM:
Idle
Card inserted by the user
Option selected
Cash withdrawal
Money transfer
Display the account balance
Note: JavaScript does not support enumerations, so we will be using the
Object.freeze()
method as an alternative that freezes an object and prevents further modifications.
User and ATM card #
The User
class stores the user's ATMcard
and bank account, where the ATMCard
class holds the card number, customer name, card expiration date, and PIN. The definitions of these classes are provided below:
Bank and bank account #
The Bank
class represents a bank having a name and code and can also add an ATM. The BankAccount
class represents a bank account that has two child classes: SavingAccount
and CurrentAccount
. These derived classes have a method for finding the withdrawal limit. The definitions of these classes are provided below:
Card reader, card dispenser, printer, screen, and keypad#
The CardReader
, CashDispenser
, Keypad
, Screen
and Printer
classes compose the ATM and have the following functionalities:
CardReader
: It reads the card inserted by the user.CashDispenser
: It dispenses cash upon withdrawal request.Keypad
: It is used by the user to enter the PIN for authentication.Screen
: It displays messages.Printer
: It prints receipts.
The definitions of these classes are provided below:
ATM state#
ATMState
is an abstract class that is extended by IdleState
, HasCardState
, SelectOperationState
, CheckBalanceState
, CashWithdrawalState
and TransferMoneyState
. All of these derived classes override the returnCard()
and exit()
functions of the ATMState
class. The derived classes individually override the following functions:
IdleState
: This class overrides theinsertCard()
function.HasCardState
: This class overrides theauthenticatePin()
function.SelectOperationState
: This class overrides theselectOperation()
function.CheckBalanceState
: This class overrides thedisplayBalance()
function.CashWithdrawalState
: This class overrides thecashWithdrawal()
function.TransferMoneyState
: This class overrides thetransferMoney()
function.
The definitions of these classes are provided below:
ATM and ATM room#
An ATMRoom
has an ATM
and a User
with the following:
A specific state at a given moment
Balance
A limited number of hundred, fifty, and ten dollar bills
The definitions of these classes are provided below:
Wrapping up#
We've explored the complete design of the ATM in this chapter. We've looked at how a basic ATM system can be visualized using various UML diagrams and designed using object-oriented principles and design patterns.
Activity Diagram for the ATM System
Getting Ready: The Chess Game